﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;
using G.WeiBo.WebAPI.Response;
using G.WeiBo.WebAPI.Request;
using G.WeiBo.WebAPI;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;

namespace G.WeiBo.View
{
    public partial class WriteWeiBoPage : GBasePage
    {

        DefaultClient client = GlobalStaticData.Client;
        public WriteWeiBoPage()
        {
            InitializeComponent();
            //var stream = App.GetResourceStream(new Uri("/G.WeiBo;component/Koala.jpg", UriKind.Relative)).Stream;
            //Buffer = new byte[stream.Length];
            //stream.Read(Buffer, 0, Buffer.Length);
            //System.Windows.Media.Imaging.BitmapImage bitmap = new System.Windows.Media.Imaging.BitmapImage();
            //bitmap.SetSource(stream);
            //this.img.Source = bitmap;
            //this.txtSize.Text = string.Format("{0} KB", (int)Buffer.Length / 1024);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (this.txtContent.Text == string.Empty)
            {
                MessageBox.Show("请输入您要发布的消息");
                txtContent.Focus();
                return;
            }
            (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = false;
            this.Focus();
            LoadingIndicator.IsIndeterminate = true;
            if (Buffer != null)
            {
                client.Execute(new UploadPicRequest { Message = this.txtContent.Text, UploadData = Buffer }, (c) =>
                {
                    if (!c.IsError)
                    {
                        this.ShowState("发布成功！");
                        this.NavigationService.GoBack();
                    }
                    LoadingIndicator.IsIndeterminate = false;
                    (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true;
                });
            }
            else
            {
                client.Execute(new UpdateStatueRequeste { Content = this.txtContent.Text }, (c) =>
                {
                    if (!c.IsError)
                    {
                        this.ShowState("发布成功！");
                        this.NavigationService.GoBack();
                    }
                    LoadingIndicator.IsIndeterminate = false;
                    (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true;
                });
            }
            //client.webUtils.oAuthWebRequestWithPic(Web.WebRequestMethod.POST, "http://api.t.sina.com.cn/statuses/upload.xml", "status=test1", buffer, (s) =>
            //{
            //    var str = s;
            //});
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            Microsoft.Phone.Tasks.PhotoChooserTask task = new Microsoft.Phone.Tasks.PhotoChooserTask();
            task.ShowCamera = true;
            task.Completed += new EventHandler<Microsoft.Phone.Tasks.PhotoResult>(task_Completed);
            task.Show();
        }
        byte[] Buffer;
        void task_Completed(object sender, Microsoft.Phone.Tasks.PhotoResult e)
        {
            if (e.TaskResult == Microsoft.Phone.Tasks.TaskResult.OK)
            {
                Buffer = new byte[e.ChosenPhoto.Length];
                e.ChosenPhoto.Read(Buffer, 0, Buffer.Length);
                System.Windows.Media.Imaging.BitmapImage bitmap = new System.Windows.Media.Imaging.BitmapImage();
                bitmap.SetSource(e.ChosenPhoto);
                this.img.Source = bitmap;
                this.txtSize.Text = string.Format("{0} KB", (int)Buffer.Length / 1024);
                stackpanel_pic.Visibility = System.Windows.Visibility.Visible;
                btnYasuo.Tag = 10;
                btnYasuo.Content = string.Format("压缩图片({0}%)", 90);
            }
        }

        private void btnYasuo_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            int quality = int.Parse(btn.Tag.ToString()) - 1;
            if (quality <= 0)
            {
                MessageBox.Show("无法进一步压缩");
                return;
            }
            if (Buffer != null)
            {
                WriteableBitmap bitmap = new WriteableBitmap(img.Source as BitmapSource);
                System.IO.Stream stream = new System.IO.MemoryStream();
                bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, (quality) * 10);
                Buffer = new byte[stream.Length];
                stream.Seek(0, System.IO.SeekOrigin.Begin);
                stream.Read(Buffer, 0, Buffer.Length);
                System.Windows.Media.Imaging.BitmapImage source = new System.Windows.Media.Imaging.BitmapImage();
                source.SetSource(stream);
                img.Source = source;
                btn.Tag = quality;
                this.txtSize.Text = string.Format("{0} KB", (int)Buffer.Length / 1024);
                btn.Content = string.Format("压缩图片({0}%)", (quality) * 10);
            }
        }


        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtContent.Text = string.Empty;
            Buffer = null;
            stackpanel_pic.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}